package com.example.yifei.ui.page.login

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.yifei.R
import com.example.yifei.data.bean.WebData
import com.example.yifei.ui.page.common.RouteName
import com.example.yifei.ui.page.widgets.MyButton
import com.example.yifei.ui.page.widgets.MyRadioButton
import com.example.yifei.ui.page.widgets.RadioButtonDefaults
import com.example.yifei.utils.RouteUtils
import com.example.yifei.utils.showToast
import kotlinx.coroutines.flow.collect

/**
 * @Author: zhudaihao
 * @Date: 2022/5/6
 * @Describe: 登录
 */
@ExperimentalComposeUiApi
@Composable
fun LoginPage(
    navCtrl: NavHostController,
    viewModel: LoginViewModel = hiltViewModel()
) {
    val viewStates = viewModel.viewStates

    Log.e("TAG", "------- LoginPage ")


    //订阅消息
    LaunchedEffect(Unit) {
        viewModel.viewEvents.collect {
            when (it) {
                is LoginViewEvent.ErrorMsg -> showToast(it.msg)
                is LoginViewEvent.Success -> RouteUtils.navTo(
                    navCtrl = navCtrl,
                    destinationName = RouteName.HOME,
                )
            }
        }
    }

    SetUi(viewStates, viewModel, navCtrl)

}

@Composable
private fun SetUi(
    viewStates: LoginViewState,
    viewModel: LoginViewModel,
    navCtrl: NavHostController
) {
    Box(modifier = Modifier.background(Color.White)) {
        Column(
            modifier = Modifier
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(modifier = Modifier.height(103.dp))
            Text(
                "登录",
                fontSize = 32.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.width(TextFieldDefaults.MinWidth)
            )
            Spacer(modifier = Modifier.height(3.dp))
            Text(
                "欢迎使用Efei易菲小程序",
                color = Color(0xff8D90A8),
                modifier = Modifier.width(TextFieldDefaults.MinWidth)
            )
            Spacer(modifier = Modifier.height(36.dp))

            //账号
            TextField(
                value = viewStates.account,
                onValueChange = {
                    viewModel.dispatch(LoginViewAction.UpdateAccount(it))
                },
                trailingIcon = @Composable {
                    Image(
                        painter = painterResource(id = R.mipmap.close),
                        contentDescription = null,
                        modifier = Modifier
                            .size(viewStates.cleanAccountSize)
                            .clickable {
                                viewModel.dispatch(
                                    LoginViewAction.CleanAccount
                                )
                            }
                    )
                },
                placeholder = @Composable {
                    Text(text = "请输入账号", fontSize = 15.sp, color = Color(0xffB4B7D0))
                },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Number
                ),

                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color(0xffF4F6FB),
                    cursorColor = Color.Gray,
                    focusedIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent
                ),
                shape = RoundedCornerShape(4.dp),
            )

            Spacer(modifier = Modifier.height(16.dp))

            //密码
            TextField(
                value = viewStates.password,
                onValueChange = {
                    viewModel.dispatch(LoginViewAction.UpdatePassword(it))
                },
                trailingIcon = @Composable {
                    Row {
                        Image(
                            painter = painterResource(viewStates.passwordRes),
                            contentDescription = null,
                            modifier = Modifier
                                .size(viewStates.cleanPasswordSize)
                                .clickable {
                                    //密码显示隐藏
                                    viewModel.dispatch(LoginViewAction.PasswordState)
                                }
                        )

                        Spacer(modifier = Modifier.width(10.dp))

                        Image(
                            painter = painterResource(id = R.mipmap.close),
                            contentDescription = null,
                            modifier = Modifier
                                .size(viewStates.cleanPasswordSize)
                                .clickable {
                                    viewModel.dispatch(LoginViewAction.CleanPassword)
                                }
                        )

                        Spacer(modifier = Modifier.width(15.dp))

                    }

                },
                placeholder = @Composable {
                    Text(text = "请输入密码", fontSize = 15.sp, color = Color(0xffB4B7D0))
                },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Password
                ),

                visualTransformation = viewStates.visualTransformation,
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color(0xffF4F6FB),
                    cursorColor = Color.Gray,
                    focusedIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent
                ),
                shape = RoundedCornerShape(4.dp),
            )

            Spacer(modifier = Modifier.height(8.dp))

            Box(
                modifier = Modifier
                    .width(TextFieldDefaults.MinWidth),
                contentAlignment = Alignment.TopEnd
            ) {
                Text(text = "忘记密码？",
                    color = Color(0xff336AF8),
                    textAlign = TextAlign.Right,
                    fontSize = 12.sp, modifier = Modifier
                        .clickable {

                        })
            }

            Spacer(modifier = Modifier.height(24.dp))


            Row(
                Modifier
                    .defaultMinSize(
                        minWidth = TextFieldDefaults.MinWidth,
                        minHeight = 48.dp
                    )
                    .background(
                        brush = Brush.horizontalGradient(
                            colors = listOf(
                                Color(0xff7AB5FF),
                                Color(0xff336AF8),
                            )
                        ), shape = RoundedCornerShape(4.dp)
                    )
                    .clickable {
                        viewModel.dispatch(LoginViewAction.Login)
                    },
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) @Composable {
                Text(
                    text = "登录",
                    color = Color.White,
                    fontSize = 16.sp,
                )
            }

            Spacer(modifier = Modifier.height(13.dp))

            //协议
            Row(verticalAlignment = Alignment.CenterVertically) {
                Spacer(modifier = Modifier.width(18.dp))
                MyRadioButton(
                    selected = viewStates.isAgreement,
                    colors = RadioButtonDefaults.colors(
                        selectedColor = Color(0xff336AF8),
                        unselectedColor = Color(0xffD7D9E6)
                    ),
                    radius = 8.dp,
                    onClick = {
                        viewModel.dispatch(LoginViewAction.UpdateAgreement)
                    })
                Text(
                    text = "点击确认，即表示阅读并同意",
                    color = Color(0xff5C5A58),
                    textAlign = TextAlign.Center,
                    fontSize = 12.sp
                )

                Spacer(modifier = Modifier.width(4.dp))

                Text(text = "《注册使用服务条款》",
                    color = Color(0xff336AF8),
                    textAlign = TextAlign.Center,
                    fontSize = 12.sp, modifier = Modifier
                        .clickable {
                            RouteUtils.navTo(
                                navCtrl = navCtrl, destinationName = RouteName.WEB_VIEW,
                                args = WebData(title = "注册使用服务条款", url = "https://www.baidu.com")
                            )
                        })
            }


        }
    }
}

